package mailsystem.persistance.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DatabaseConnection {
	private final String connectionURL = "jdbc:derby:data/MailSystem";
	private final String connectionTestURL = "jdbc:derby:data/MailSystemTestDB";	
	public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
	
	private Connection connection = null;
	
	public DatabaseConnection() {		
		try {
			Class.forName(driver).newInstance();
			connection = DriverManager.getConnection(connectionURL);
		} catch (Exception e) {
			connection = null;			
		}		
	}

	public Connection getConnection() {
		return connection;
	}

	public PreparedStatement prepareStatement(String sql) {
		try {
			return connection.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void changeToTestMode() {
		try {
			Class.forName(driver).newInstance();
			connection = DriverManager.getConnection(connectionTestURL);
		} catch (Exception e) {
			connection = null;
		}		
	}
}
